﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Windows.Forms;
using SmartMD.DataModel.Entity;
using SmartMD.DataAccess.Service;
using Techstar.Dialog;
using SmartMD.Manager.Common;
using SmartMD.Manager.Utils;
using Duoease.ContainerModel.Utils;
using Duoease.ContainerModel.Entity;
using Duoease.ContainerModel;
using SmartMD.DataModel;

namespace SmartMD.Manager.Container
{
    /// <summary>
    /// Interaction logic for ContainerModelPanel.xaml
    /// </summary>
    public partial class ContainerModelPanel : BaseWindow
    {
        //added by xyl 2012-05-22
        public static ContainerModelEntity modelEntity = null;

        public static string ModuleName { get { return "容器建模"; } }
        public BoxModel boxModel = new BoxModel();
        public FridgeModel fridgeModel;
        public ShelfModel shelfModel = new ShelfModel();

        public ContainerModelPanel()
        {
            InitializeComponent();
            //注册控件条
            this.RegisterToolbar(this.tbBasicDataTool);

            CM.fridgeClickHandler += new MouseEventHandler(CM_IceModelMousedown);
            CM.shelfClickHandler += new MouseEventHandler(CM_DrawerModelMousedown);
            CM.MsgViewClick += new EventHandler(CM_MsgViewClick);
            CM.MsgGetOutClick += new EventHandler(CM_MsgGetOutClick);
            CM.MsgLoadClick += new EventHandler(CM_MsgLoadClick);
            CM.IsShowContext = true;
            cmw = CM;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //读取XML画模型
            XmlDocument doc = new XmlDocument();
            //装入指定的XML文档
            /*doc.Load(System.Windows.Forms.Application.StartupPath + "\\bb.xml");
            CM.Xml = doc;
            fridgeModel = CM.GetMsg(ContainerModel.ContainerModel.model_style.IceModel) as ContainerModel.IceModel;

            //测试冰箱梅格存放容量状态
            if (fridgeModel.Row >= 2 & fridgeModel.Column >= 2)
            {
                double[,] d = new double[fridgeModel.Row, fridgeModel.Column];
                d[0, 0] = 0.1;
                d[0, 1] = 0.5;
                d[1, 0] = 0.0;
                d[1, 1] = 0.8;
                CM.IceModel = fridgeModel;
                CM.IceFullPercent = d;
            } */
        }

        #region 控件事件
        protected void CM_IceModelMousedown(object sender, MouseEventArgs e)
        {
            //测试架子第1行第2列存放容量状态
            /*  shelfModel = CM.GetMsg(ContainerModel.ContainerModel.model_style.DrawerMdoel) as ContainerModel.DrawerModel;
              if (shelfModel != null)
              {
                  int row = CM.MouseColumn;
                  int column = CM.MouseRow;
                  if (row == 2 && column == 2)
                  {
                      if (shelfModel.Row >= 2 && shelfModel.Column >= 2)
                      {
                          double[,] d = new double[shelfModel.Row, shelfModel.Column];
                          d[0, 0] = 0.3;
                          d[0, 1] = 0.7;
                          d[1, 0] = 0.4;
                          d[1, 1] = 1.0;
                          CM.DrawerFullPercent = d;
                      }
                  }
                  else if (row == 1 && column == 2)
                  {
                      double[,] d = new double[shelfModel.Row, shelfModel.Column];
                      d[0, 0] = 0.5;
                      d[0, 1] = 0.9;
                      CM.DrawerFullPercent = d;
                  }
                  else
                  {
                      CM.DrawerFullPercent = null;

                  }
              } */

            //黄俊修改 存储状态
            //double[,] FullPercent = CM.DrawerFullPercent;
            //CM.DrawerFullPercent = FullPercent;
            //CM.GetModelPercent();
        }

        protected void CM_DrawerModelMousedown(object sender, MouseEventArgs e)
        {
            //盒子第2行第2列存放样本状态
            /*   boxModel = CM.GetMsg(ContainerModel.ContainerModel.model_style.BoxModel) as ContainerModel.BoxModel;
               if (boxModel != null)
               {
                   int row = CM.MouseColumn;
                   int column = CM.MouseRow;
                   if (row == 2 && column == 2)
                   {
                       if (shelfModel.Row >= 2 && shelfModel.Column >= 2)
                       {
                           bool[,] v = new bool[boxModel.Row, boxModel.Column];
                           v[0, 0] = true;
                           v[0, 1] = false;
                           v[1, 0] = true;
                           v[1, 1] = false;
                           CM.BoxIsFull = v;
                       }
                   }
                   else
                   {
                       CM.BoxIsFull = null;

                   }
               } */

            //黄俊修改 存储状态         
            //bool[,] IsFull = CM.BoxIsFull;
            //CM.BoxIsFull = IsFull;
            //CM.GetModelPercent();
        }
        //黄俊修改
        protected void CM_MsgViewClick(object sender, EventArgs e)
        {
            if (CME == null && EditCME != null)
            {
                CME = EditCME;
            }
            string strPosition = CM.BoxModel.ID + "-" + ModelUtil.GetLetter(CM.MouseRow) + CM.MouseColumn.ToString();
            SpecimenMsg SM = new SpecimenMsg(SpecimenMsg.SpecimenStyle.GetMsg, CM, CME);
            if (SM.IsShow)
            {
                SM.Show();
            }
        }
        protected void CM_MsgGetOutClick(object sender, EventArgs e)
        {
            if (CME == null && EditCME != null)
            {
                CME = EditCME;
            }
            string strPosition = CM.BoxModel.ID + "-" + ModelUtil.GetLetter(CM.MouseRow) + CM.MouseColumn.ToString();
            SpecimenMsg SM = new SpecimenMsg(SpecimenMsg.SpecimenStyle.RemoveMsg, CM, CME);
            SM.SaveModelClick += new RoutedEventHandler(BtnSave_Click);
            if (SM.IsShow)
            {
                SM.Show();
            }
        }
        protected void CM_MsgLoadClick(object sender, EventArgs e)
        {
            if (CME == null && EditCME != null)
            {
                CME = EditCME;
            }
            string strPosition = CM.BoxModel.ID + "-" + ModelUtil.GetLetter(CM.MouseRow) + CM.MouseColumn.ToString();
            SpecimenMsg SM = new SpecimenMsg(SpecimenMsg.SpecimenStyle.InputMsg, CM, CME);
            SM.SaveModelClick += new RoutedEventHandler(BtnSave_Click);
            SM.Show();
        }
        #endregion

        NewContainerModelDialog createModelDlg = null;
        private void btnNewItem_Click(object sender, RoutedEventArgs e)
        {
            CME = null;
            EditCME = null;
            CM.IsCreatNewModel = true;

            CM.RefreshAllLabel();

            createModelDlg = new NewContainerModelDialog();
            createModelDlg.Owner = this;
            createModelDlg.txtBorderWidth.Text = "1";
            createModelDlg.txtTransparency.Text = "100";
            createModelDlg.ShowDialog();
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            bool success = false;
            XmlNode root = this.CM.docmodel.Xml.SelectSingleNode("Container");
            XmlElement xe = root as XmlElement;
            string specification = this.CM.fridgeSpecification + "-" + CM.shelfSpecification + "-" + CM.boxSpecification;
            xe.SetAttribute("Specification", specification);
            string stylestr = xe.OuterXml.ToString();
           // string styleXml = this.CM.docmodel.Xml.InnerXml.ToString();

            if (IsEdit && EditCME != null)
            {
                success = DataServiceManager.ContainerModelService.SaveContainerModel(EditCME);
                EditCME.StyleFile = CompressionHelper.CompressToByte(stylestr);
                success = DataServiceManager.ContainerModelService.SaveContainerModel(EditCME);
                IsEdit = false;
                EditCME = null;
            }
            else
            {
                ContainerModelEntity entity = new ContainerModelEntity();
                entity.Id = System.Guid.NewGuid().ToString();
                entity.StyleFile = CompressionHelper.CompressToByte(stylestr);
                entity.CreateDate = System.DateTime.Now;
                if (createModelDlg == null)
                {
                    entity.Name = modelEntity.Name;
                    entity.ModelType = modelEntity.ModelType;
                }
                else
                {
                    entity.Name = (createModelDlg.txtName.SelectedItem as ComboBoxValueItem).Name.ToString(); // createModelDlg.txtName.Text;
                    string modeltype = (createModelDlg.cmbModelType.SelectedItem as ComboBoxValueItem).Value.ToString();
                    entity.ModelType = ContainerModelEntity.GetStyle(modeltype);
                   
                }
                success = DataServiceManager.ContainerModelService.SaveContainerModel(entity);
                CME = entity;
            }

            if (success)
            {
                string message = "容器模型保存成功！";
                TaskDialogEx.Information("提示", message);
            }
            else
            {
                string message = "容器模型不能保存！";
                TaskDialogEx.Information("保存失败", message);
            }
        }

        public ContainerModelEntity CME = null;
        public static bool IsEdit = false;
        public static ContainerModelEntity EditCME = null;
        private static ContainerModelView cmw = new ContainerModelView();
        public static void LoadModel(ContainerModelEntity item)
        {
            if (item == null)
                return;
            modelEntity = item;

                cmw.RefreshAllLabel();

                cmw.FridgeModel = null;
                cmw.ShelfModel = null;
                cmw.BoxModel = null;
                cmw.IsShowContext = true;
                cmw.IsCreatNewModel = true;

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(CompressionHelper.DeCompressToString(item.StyleFile));

                //装入指定的XML文档
                //doc.Load(System.Windows.Forms.Application.StartupPath + "\\bb.xml");
                cmw.docmodel.Xml = doc;

                BaseModel bm = (BaseModel)cmw.ParseXml(item.ModelType);
                cmw.FridgeModel = bm;
        }

        public static void ClearModel()
        {
            cmw.IsCreatNewModel = true;
            cmw.RefreshAllLabel();
        }
    }
}
